//
//  ShowCollectionViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/9/3.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ShowCollectionViewCell.h"
#import "ShowModel.h"
#import "ZWYNetTool.h"
#import "ShowTableViewCell.h"
#import "UIView+SDAutoLayout.h"
#import "UIImageView+WebCache.h"
#import "NSString+Vali.h"
#import "ShowRes.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "ShowDetailViewController.h"
#import "ShowViewController.h"
#import "NSString+HX_TimeInterval.h"


#define kShowTableViewCellName @"ShowTableViewCellNameReuse"
@interface ShowCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) ShowModel *model;
@property (nonatomic, strong) UIImageView *photo;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *time;

@end

@implementation ShowCollectionViewCell

- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray new];
    }
    return _dataArray;
}

- (void)setNum:(NSInteger)num {
    _num = num;
    if (_num == 0) {
        _num = 2;
    } else if (_num == 1) {
        _num = 0;
    } else if (_num == 2) {
        _num = 1;
    } else if (_num == 3) {
        _num = 4;
    } else if (_num == 4) {
        _num = 3;
    }
    NSString *string = [NSString stringWithFormat:@"lastid=0&order=%ld&orderPraise=1", _num];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"30"};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=show&a=getlist251" andBody:string andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [ShowModel modelWithDic:result];
        _dataArray = _model.res;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = NO;
    [_tableView registerClass:[ShowTableViewCell class] forCellReuseIdentifier:kShowTableViewCellName];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowRes *model = self.dataArray[indexPath.section];
    return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"res" cellClass:[ShowTableViewCell class] contentViewWidth:self.bounds.size.width];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _model.res.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kShowTableViewCellName];
    if(_model) {
        cell.res = _model.res[indexPath.section];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    _photo = [UIImageView new];
    
    _name = [UILabel new];
    _name.font = [UIFont systemFontOfSize:14];
    
    _time = [UILabel new];
    _time.font = [UIFont systemFontOfSize:14];
    [view sd_addSubviews:@[_photo, _name, _time]];
    CGFloat margin = 10;
    _photo.sd_layout
    .leftSpaceToView(view, margin)
    .centerYEqualToView(view)
    .widthIs(40)
    .heightEqualToWidth();
    
    _name.sd_layout
    .leftSpaceToView(_photo, margin)
    .centerYEqualToView(_photo)
    .heightIs(20);
    [_name setSingleLineAutoResizeWithMaxWidth:150];
    
    _time.sd_layout
    .rightSpaceToView(view, margin)
    .centerYEqualToView(_photo)
    .heightIs(20);
    [_time setSingleLineAutoResizeWithMaxWidth:150];
    
    _photo.contentMode = UIViewContentModeScaleAspectFill;
    _photo.clipsToBounds = YES;
    _photo.layer.masksToBounds = YES;
    _photo.layer.cornerRadius = _photo.bounds.size.width / 2;
    [_photo sd_setImageWithURL:[NSURL URLWithString:[NSString imageWebWithStr:_model.res[section].headimg]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    _name.text = _model.res[section].nickname;
    if (_model.res[section].addtime) {
        _time.text = [NSString updateTimeForTimeIntervalString:_model.res[section].addtime];
    };
    
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowDetailViewController *vc = [[ShowDetailViewController alloc] init];
    ShowViewController *viewController = (ShowViewController *)self.nextResponder.nextResponder.nextResponder;
    vc.idString = _model.res[indexPath.section].show_id;
    [viewController.navigationController pushViewController:vc animated:YES];
}

@end
